//
//  LogonViewController.m
//  UI_豆瓣2.0
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LogonViewController.h"
#import "LogonView.h"
#import "MBProgressHUD.h"
#import "DataBaseSingleton.h"

@interface LogonViewController ()<MBProgressHUDDelegate>

@property (nonatomic, retain)LogonView *logonV;
@property (nonatomic, retain)MBProgressHUD *hud;

@end

@implementation LogonViewController

- (void)dealloc
{
    [_hud release];
    [_logonV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    
    self.logonV = [[LogonView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.logonV];
    [_logonV release];
}

// 右按钮
- (void)rightAction:(UIBarButtonItem *)button{
    if (![self.logonV.userNameTextField.text isEqualToString:@""] && ![self.logonV.passwordTextField.text isEqualToString:@""] && ![self.logonV.passwordTwoTextField.text isEqualToString:@""] && [self.logonV.passwordTextField.text isEqualToString:self.logonV.passwordTwoTextField.text]){
        
        NSMutableArray *array = [DATABASE selectUserName:self.logonV.userNameTextField.text];
        if (array.count == 0) {
            User *user = [[User alloc] init];
            user.userName = self.logonV.userNameTextField.text;
            user.password = self.logonV.passwordTextField.text;
            user.email = self.logonV.emailTextField.text;
            user.phoneNumber = self.logonV.phoneNumberTextField.text;
            [DATABASE insertUser:user];
            [user release];
        
            self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.hud.labelText = @"注册成功";
            self.hud.mode = MBProgressHUDModeText;
            self.hud.delegate = self;
            [self.hud hide:YES afterDelay:1];
        
        } else {
        
            self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.hud.labelText = @"用户名已存在";
            self.hud.mode = MBProgressHUDModeText;
            //            self.hud.delegate = self;
            [self.hud hide:YES afterDelay:1];
        }
    }
}
#pragma mark -- hud消失时触发的方法
- (void)hudWasHidden:(MBProgressHUD *)hud{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
